package com.qianfeng.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianfeng.biz.IGoodsInfoBiz;
import com.qianfeng.biz.impl.GoodsInfoBizImpl;
import com.qianfeng.commons.WebUtils;
import com.qianfeng.dto.PageBean;
import com.qianfeng.entity.GoodInfo;
import com.qianfeng.entity.GoodType;

public class GoodsInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private IGoodsInfoBiz giBiz = new GoodsInfoBizImpl();
	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		PrintWriter out = response.getWriter();
		if("queryById".equals(action)){
			//通过商品id查询商品对象
			int goodId = WebUtils.StringToInt(request.getParameter("id"),-1);
			GoodInfo gi = giBiz.queryGoodInfoById(goodId);
			request.setAttribute("goodInfo", gi);
			request.getRequestDispatcher("introduction.jsp").forward(request, response);
		}else if("queryAllGoodsInfo".equals(action)){
			queryfenye(request, response);
		}else if("toUpdate".equals(action)){
			int goodId = WebUtils.StringToInt(request.getParameter("gid"),-1);
			GoodInfo gi = giBiz.queryGoodInfoById(goodId);
			request.getSession().setAttribute("goodInfo", gi);
			response.sendRedirect("goods/goodupdate.jsp");
		}else if("update".equals(action)){
		update(request, out);
		}else if("delete".equals(action)){
			int id = WebUtils.StringToInt(request.getParameter("id"), -1);
			
			int result = giBiz.deleteById(id);
			if(result>0){
				out.write("<script>alert('删除类别信息成功');location.href='GoodsInfoServlet?action=queryAllGoodsInfo&currentPage=1'</script>");
			}else{
				out.write("<script>alert('删除类别信息失败');location.href='GoodsInfoServlet?action=queryAllGoodsInfo&currentPage=1'</script>");
			}
		}
		
	}

	private void update(HttpServletRequest request, PrintWriter out) {
		int id = WebUtils.StringToInt(request.getParameter("id"), -1);
		String name = request.getParameter("goods_name");
		int paerentid = WebUtils.StringToInt(request.getParameter("goods_parentid"), -1);;
		int fatherid = WebUtils.StringToInt(request.getParameter("goods_fatherid"), -1);
		String goodprice = request.getParameter("goods_price");
		Double goodprices = Double.valueOf(goodprice);
		double gp = goodprices.doubleValue();
		String de = request.getParameter("goods_description");	
		int stock = WebUtils.StringToInt(request.getParameter("goods_stock"), -1);
		GoodInfo goodInfo = new GoodInfo();
		goodInfo.setId(id);
		goodInfo.setGoods_name(name);
		goodInfo.setGoods_parentid(paerentid);
		goodInfo.setGoods_fatherid(fatherid);
		goodInfo.setGoods_price(gp);
		goodInfo.setGoods_description(de);
		goodInfo.setGoods_stock(stock);
		int result = giBiz.updateGoodInfo(goodInfo);
		if(result>0){
			out.write("<script>alert('修改类别信息成功');location.href='GoodsInfoServlet?action=queryAllGoodsInfo&currentPage=1'</script>");
		}else{
			out.write("<script>alert('修改类别信息失败');location.href='GoodsInfoServlet?action=queryAllGoodsInfo&currentPage=1'</script>");
		}
	}

	private void queryfenye(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cp = request.getParameter("currentPage");
		int currPage = Integer.parseInt(cp);
		PageBean page=giBiz.queryall(currPage);
		request.getSession().setAttribute("page", page);

		response.sendRedirect("goods/goodsList.jsp");
	}

}
